package com.cu.employmentmanagementsystem.service.impl;

import com.cu.employmentmanagementsystem.dao.CompanyDao;
import com.cu.employmentmanagementsystem.dao.RecruitInformationDao;
import com.cu.employmentmanagementsystem.dao.StudentRecruitmentDao;
import com.cu.employmentmanagementsystem.dao.StudentVitaDao;
import com.cu.employmentmanagementsystem.dto.StudentVitaDTO;
import com.cu.employmentmanagementsystem.entity.Company;
import com.cu.employmentmanagementsystem.entity.RecruitInformation;
import com.cu.employmentmanagementsystem.entity.StudentRecruitment;
import com.cu.employmentmanagementsystem.entity.StudentVita;
import com.cu.employmentmanagementsystem.service.CompanyService;
import com.cu.employmentmanagementsystem.util.CommonUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.List;

/**
 * 公司信息表(Company)表服务实现类
 *
 * @author makejava
<<<<<<< HEAD
 * @since 2024-07-02 14:00:06
=======
 * @since 2024-07-02 13:14:02
>>>>>>> origin/master
 */
@Service("companyService")
public class CompanyServiceImpl implements CompanyService {
    @Resource
    private CompanyDao companyDao;

    @Resource
    private RecruitInformationDao recruitInformationDao;

    @Resource
    private StudentVitaDao studentVitaDao ;

    @Resource
    private StudentRecruitmentDao studentRecruitmentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Company queryById(Long id) {
        return this.companyDao.queryById(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Company queryByCompanyId(Long id) {
        return this.companyDao.queryByCompanyId(id);
    }

    /**
     * 分页查询
     *
     * @param company 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Company> queryByPage(Company company, PageRequest pageRequest) {
        long total = this.companyDao.count(company);
        return new PageImpl<>(this.companyDao.queryAllByLimit(company, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param company 实例对象
     * @return 实例对象
     */
    @Override
    public Company insert(Company company) {
        this.companyDao.insert(company);
        return company;
    }

    /**
     * 修改数据
     *
     * @param company 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean update(Company company) {
        return this.companyDao.update(company) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.companyDao.deleteById(id) > 0;
    }


    /**
     * 根据条件查询所有公司信息
     *
     * @param company 查询条件
     * @return 匹配的公司信息列表
     */
    @Override
    public List<Company> queryAllByCondition(Company company)
    {
        return this.companyDao.queryAllByCondition(company);
    }


    /**
     * 查询指定公司的所有招聘信息
     *
     * @return 招聘信息列表
     */
    @Override
    public List<RecruitInformation> queryRecruitments() {
        Long companyId = CommonUtils.getCompanyId();
        return this.recruitInformationDao.queryBycompanyId(companyId);
    }


    /**
     * 根据招聘ID查询相关的学生简历信息
     *
     * @param recruitmentId 招聘ID
     * @return 学生简历信息列表，包含与指定招聘ID相关的所有学生简历信息
     */
    @Override
    public List<StudentVitaDTO> queryResumesByRecruitmentId(Long recruitmentId) {
        List<StudentVitaDTO> studentVitaDTOList = this.studentVitaDao.queryResumesByRecruitmentId(recruitmentId);
        studentVitaDTOList.forEach(item->
        {
            StudentRecruitment studentRecruitment = studentRecruitmentDao.queryByRecruitIdAndResumeId(recruitmentId,item.getId());
            item.setDeliveryStatus(studentRecruitment.getDeliveryStatus());
        });
        return studentVitaDTOList;
    }

    /**
     * 确认学生投递状态（接受简历）
     *
     * @param recruitmentId 招聘ID
     * @param resumeId      学生简历ID
     * @return 如果更新成功返回true，否则返回false
     */
    @Override
    public boolean acDeliveryStateByRecruitmentIdAndResumeId(Long recruitmentId, Long resumeId) {
        return this.studentRecruitmentDao.acDeliveryStateByRecruitmentIdAndResumeId(recruitmentId,resumeId) > 0;
    }

    /**
     * 拒绝学生投递状态（拒绝简历）
     *
     * @param recruitmentId 招聘ID
     * @param resumeId      学生简历ID
     * @return 如果更新成功返回true，否则返回false
     */
    @Override
    public boolean refuseDeliveryStateByRecruitmentIdAndResumeId(Long recruitmentId, Long resumeId) {
        return this.studentRecruitmentDao.refuseDeliveryStateByRecruitmentIdAndResumeId(recruitmentId,resumeId) > 0;
    }

    @Override
    public List<Company> getAllCompanies()
    {
        return this.companyDao.queryAllByCondition(null);
    }
}
